<template>
  <view>
    <view class="search_top" >
        <my-search @goSearch="goSearch"></my-search>
    </view>
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="(val,i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'../../subpkg/goods_detail/goods_detail?goods_id='+val.goods_id">
          <image :src="val.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航区域 -->
    <view class="nav_box">
      <view class="nav_list" v-for="(val,i) in navList" :key="i" @click="navClick(val)">
        <image :src="val.image_src"></image>
      </view>
    </view>
    <!--渲染楼层 -->
    <view class="floor">
      <!-- 标题 -->
      <view class="floor_list" v-for="(val,i) in floorList" :key="i">
        <image :src="val.floor_title.image_src" mode="" class="floor_title"></image>
        <!-- 图片 -->
        <view class="floor_img">
          <!-- 左侧大图 -->
          <navigator class="left_img" :url="val.product_list[0].url">
            <image  :src="val.product_list[0].image_src" :style="{width:val.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
          </navigator>
          <view class="right_img">
            <navigator :url="val2.url" v-for="(val2,i2) in val.product_list" :key="i2">
            <image  :src="val2.image_src" v-if="i2!=0"   :style="{width:val2.image_width+'rpx'}" mode="widthFix"></image></navigator>
          </view>
        </view>
      </view>
    </view>
  </view>

</template>

<script>
  import tabbarBadge from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[tabbarBadge],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      }
    },
    onLoad() {
      // 发起数据请求
      this.getList()
      this.getNav()
      this.getFloor()
    },

    methods: {
      async getList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message
      },
      async getNav() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      async getFloor() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // 处理数据
        res.message.forEach(item=>{
          item.product_list.forEach(val=>{
            val.url="/subpkg/goods_list/goods_list?"+val.navigator_url.split("?")[1]
          })
        })
        this.floorList = res.message
        
      },
      navClick(item) {
        if (item.name === "分类") {
          // 只能用switchBar跳转页面
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      // 切换首页
      goSearch(){
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .search_top{
    position: sticky;
    top: 0;
    z-index: 999;
  }
  swiper {
    height: 300rpx;

    .swiper-item,
    image {
      height: 100%;
      width: 100%;
    }
  }

  .nav_box {
    display: flex;
    justify-content: space-around;
    margin: 10px 0;

    .nav_list {
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }

  .floor_title {
    width: 100%;
    height: 60rpx;
  }

  .floor_img {
    display: flex;
    padding-left: 10rpx;

    .right_img {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }
</style>
